package com.ponxu.mb.web.action.admin;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ponxu.log.Log;
import com.ponxu.log.LogFactory;
import com.ponxu.mb.model.Comment;
import com.ponxu.mb.model.PageInfo;
import com.ponxu.mb.model.Post;
import com.ponxu.mb.model.User;
import com.ponxu.mb.service.CommentService;
import com.ponxu.mb.service.OptionService;
import com.ponxu.mb.service.PostService;
import com.ponxu.mb.web.action.MiniBlogAction;
import com.ponxu.web.core.WebContext;

/**
 * 评论 管理
 * 
 * @author xwz
 */
public class CommentAction extends MiniBlogAction {
	private static final Log LOG = LogFactory.getLog();
	private static final String FTL_COMMENT = "/admin/comment.ftl";
	private static final String FTL_COMMENT_EDIT = "/admin/comment-edit.ftl";
	private static final String FTL_MSG = "/admin/msg.ftl";

	/** 默认查询 */
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		WebContext context = new WebContext(request, response);
		int postId = context.getIntParameter("postId");
		LOG.debug("postId=%d", postId);

		List<Comment> list = null;
		Post post = null;

		PageInfo pageInfo = new PageInfo(request);
		if (postId == 0) {
			list = CommentService.searchComments(pageInfo, null);
		} else {
			post = PostService.getById(postId);
			list = CommentService.searchCommentsByPostId(pageInfo, postId);
		}

		request.setAttribute("page", pageInfo);
		context.put("comments", list);
		context.put("post", post);
		context.put("postId", postId);

		return FTL_COMMENT;
	}

	/**
	 * 保存
	 */
	public String save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WebContext context = new WebContext(request, response);
		String msg = "保存成功!";

		int id = context.getIntParameter("id");
		int postId = context.getIntParameter("postId");
		int parentId = context.getIntParameter("parentId");
		String content = context.getParameter("content");
		User user = (User) context.sessionGet("admin");

		Comment comment = null;
		if (id > 0) {
			comment = CommentService.getById(id);
		} else {
			comment = new Comment();
			comment.setUserId(user.getId());
			comment.setAuthorEmail(user.getEmail());
			comment.setAuthorName(user.getName());
			comment.setAuthorUrl(OptionService.read("blog_url"));
			comment.setCommentTime(new Date());
		}

		comment.setContent(content);
		comment.setParentId(parentId);
		comment.setPostId(postId);

		int i = 0;
		if (id > 0) {
			i = CommentService.update(comment);
		} else {
			i = CommentService.add(comment);
		}

		if (i == 0) {
			msg = "保存失败!";
		}

		context.put("msg", msg);
		return FTL_MSG;
	}

	private int[] getIds(HttpServletRequest request) {
		String[] idstr = request.getParameterValues("id");
		int size = idstr.length;
		int[] ids = new int[size];
		for (int i = 0; i < size; i++) {
			ids[i] = Integer.parseInt(idstr[i]);
		}
		return ids;
	}

	/**
	 * 评论删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String delete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		WebContext context = new WebContext(request, response);
		int postId = context.getIntParameter("postId");
		int[] ids = getIds(request);
		CommentService.deleteById(ids);

		context.put("msg", "删除成功!");
		context.forward("/admin/comment?oper=&postId=" + postId);
		return null;
	}

	/**
	 * 编辑评论
	 */
	public String edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WebContext context = new WebContext(request, response);
		int id = context.getIntParameter("id");
		int parentId = context.getIntParameter("parentId");
		int postId = context.getIntParameter("postId");

		if (id == 0 && parentId == 0) {
			// 普通新增
		} else if (id == 0 && parentId > 0) {
			// 回复评论 新增
			context.put("parentComment", CommentService.getById(parentId));
		} else if (id > 0) {
			// 编辑现有
			Comment comment = CommentService.getById(id);
			context.put("comment", comment);
			parentId = comment.getParentId();
		}

		context.put("id", id);
		context.put("parentId", parentId);
		context.put("post", PostService.getById(postId));

		return FTL_COMMENT_EDIT;
	}
}